#!/bin/bash
#
# jenkins-sqlsmith_execute(job) 的execute shell脚本
# 上游安装数据库的job传参：db_port/sqlsmith_database/sqlsmith_user/userpasswd

sqlsmith_path="/data/sqlsmith"

function pre_check()
{
    if [[ ! -d $sqlsmith_path ]]
    then
        echo "sqlsmith路径：$sqlsmith_path 不存在，请检查！"
        return -1
    fi

    echo "[profile]" > $WORKSPACE/profile.conf
    echo "db_port=$db_port" >> $WORKSPACE/profile.conf
    echo "sqlsmith_database=$sqlsmith_database" >> $WORKSPACE/profile.conf
    echo "sqlsmith_user=$sqlsmith_user" >> $WORKSPACE/profile.conf
    echo "database_ip=$database_ip" >> $WORKSPACE/profile.conf

    return $?
}

function run_sqlsmith()
{
    cd $sqlsmith_path
    echo "python3 -u $sqlsmith_path/sqlsmith_execute.py -d $WORKSPACE/profile.conf"
    python3 -u $sqlsmith_path/sqlsmith_execute.py -d $WORKSPACE/profile.conf
    return_code=$?
    echo $return_code

    if [[ $return_code -ne 0 ]]
    then
        date
        echo "sqlsmith 异常退出！"
        return -1
    fi

    return $?
}

function main(){
    pre_check
    if [ $? -ne 0 ]
    then
        echo "环境检查|配置文件准备失败"
        return -1
    else
        echo "环境检查|配置文件准备成功"
    fi

    run_sqlsmith
    if [ $? -ne 0 ]
    then
        echo "sqlsmith 异常！"
        echo "磁盘使用情况："
        df -h
        exit 1
    fi
    exit 0

}
